import Vue from 'vue'
import { getUserInfo } from "@/api/user"
import store from "../store"
import cookie from "@/utils/store/cookie"
import { VUE_APP_API_URL } from "@/config"

export const handleGetUserInfo = (data) => {
	// getUserInfo({
	// 	token:uni.getStorageSync('nansha_token')
	// }).then(res => {
	uni.setStorageSync('userInfo', data)
	uni.reLaunch({
		url: '/'
	})
	// })
}

export function auth() {
	if (uni.getStorageSync('nansha_token')) {
		return true
	}
	return false
}

const getImageInfo = (images) => {
	return new Promise((resolve, reject) => {
		let imageAry = {}
		images.map((item, index) => {
			uni.getImageInfo({
				src: item,
				fail: function (res) {
					imageAry[index] = null
					if (imageAry.length == images.length) {
						resolve(imageAry)
					}
				},
				success: function (res) {
					imageAry[index] = res
					if (Object.keys(imageAry).length == images.length) {
						resolve(imageAry)
					}
				}
			})
		})
	})
}

/**
 * 生成随机文件名称
 * 规则八位随机字符，加下划线连接时间戳
 */
export function getFileName (){
	function rx() {
	    return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1)
	}
	return `${+new Date()}_${rx()}${rx()}`
}

// base64转blob
export function dataURLtoBlob(dataurl) {

    var arr = dataurl.split(','),
        mime = arr[0].match(/:(.*?);/)[1],
        bstr = atob(arr[1]),
        n = bstr.length,
        u8arr = new Uint8Array(n);
    while (n--) {
        u8arr[n] = bstr.charCodeAt(n);
    }
    return new Blob([u8arr], { type: mime });
}

//ArrayBuffer ---> Buffer
export function toBuffer(ab) {
    var buf = new Buffer(ab.byteLength);
    var view = new Uint8Array(ab);
    for (var i = 0; i < buf.length; ++i) {
        buf[i] = view[i];
    }
    return buf;
}

// 选择文件
export function chooseImageBase(callback) {
	uni.chooseImage({
		count: 1,
		sourceType: ["album"],
		success: res => {
			uni.getImageInfo({
				src: res.tempFilePaths[0],
				success: image => {
					 const reader = new FileReader();
					 let Imgres = reader.readAsDataURL(res.tempFiles[0]);
					 reader.onloadend = function () {
						// 把图片转base64
						 var strBase64 = reader.result.substring(0);
						 let data={
							 "url":strBase64,
							 "imgInfo":image
						 };
						 callback(data);
					 };
				}
			});
		}
	});
}


/**
 * 判断是否是空
 * @param value
 */
export function isDefine(value) {
  if (value === null || value === "" || value === "undefined" || value === undefined || value === "null" || value === "(null)" || value === 0 || value === '0' || value === 'NULL' || typeof (value) === 'undefined' || value.length === 0) {
    return false;
  } else {
    value = value + "";
    value = value.replace(/\s/g, "");
    if (value == "") {
      return false;
    }
    return true;
  }
}

/**
 * 判断是否是空, 允许是0
 * @param value
 */
export function isDefineZero(value) {
  if (value === null || value === "" || value === "undefined" || value === undefined || value === "null" || value === "(null)"  || value === 'NULL' || typeof (value) === 'undefined') {
    return false;
  } else {
    value = value + "";
    value = value.replace(/\s/g, "");
    if (value == "") {
      return false;
    }
    return true;
  }
}

/**
 * 检查手机号码
 *
 * @param phone
 * @returns {*}
 */
export function checkPhone(phone){
  return /^1[3456789]\d{9}$/.test(phone);
}


/**
 * 检查邮箱
 *
 * @param value
 * @returns {*}
 */
 export function IsEmail(str) {
	return  /^([a-zA-Z]|[0-9])(\w|\-)+@[a-zA-Z0-9]+\.([a-zA-Z]{2,4})$/.test(str);
 }

/**
 * 生成随机数
 * @bgVal value
 */
export function randomString (len = 3) {
  const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
  const maxPos = chars.length
  let str = ''
  for (let i = 0; i < len; i++) {
    str += chars.charAt(Math.floor(Math.random() * maxPos))
  }
  return str
}
